<?php 
/**
 * 使用phpmailer发送邮件
 * @param string $to 收件人
 * @param string $subject 主题
 * @param string $body 正文
 * @param string $attachment_dir 附件地址
 * @return boolean
 */
function send_mail($to,$subject,$body='',$attachment_dir=''){
    require_once __ROOT__.'Public/PHPMailer/class.phpmailer.php';
    $mail = new PHPMailer;
    $mail->CharSet ="utf-8";//设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->IsSMTP(); // 设定使用SMTP服务
    $mail->SMTPDebug  = 1;// 启用SMTP调试功能1 = errors and messages 2 = messages only
    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
    $mail->SMTPSecure = "smtp";                 // 安全协议
    $system = M('system')->where(array('type'=>'contact'))->select();
    $config = array();
    foreach ($system as $v){
        $config[$v['name']] = $v['value'];
    }
    
    $mail->Host       = $config['host'];      // SMTP 服务器
    $mail->Port       = $config['port'];                   // SMTP服务器的端口号
    $mail->Username   = $config['username'];  // SMTP服务器用户名
    $mail->Password   = $config['password'];            // SMTP服务器密码
    $mail->From = $config['email'];
    //$mail->AddReplyTo($config['address'],get_config('base','website'));//增加回复标签，参数1地址，参数2名称
    
    //组装新的格式    标题 收件人 内容  附件
    $mail->Subject = $subject;  //主题
    $mail->MsgHTML($body); //正文  支持html格式
    $mail->AddAddress($to, " ");//增加收件人 参数1为收件人邮箱，参数2为收件人称呼
    if(!empty($attachment_dir)){
        $mail->AddAttachment($attachment_dir);//附件的路径和附件名
    }
    
    return $mail->Send();
}

function sendCurlPost($url, $dataObj) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    $ret = curl_exec($curl);
    if (false == $ret) {
        // curl_exec failed
        $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
    } else {
        $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if (200 != $rsp) {
            $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp . " " . curl_error($curl) ."\"}";
        } else {
            $result = $ret;
        }
    }
    curl_close($curl);
    return $result;
}


function getComment($blog_id,$comment_id){

    // 获取当前博客下除最后一篇的所有评论
    $data = M('comment')->where(array('blog_id'=>$blog_id,'is_last'=>0))->select();
    
    echo getstr($data, $comment_id);
}

function getstr($data,$pid,&$i = 0,$str = ''){
    
    foreach ($data as $v){
        if ($v['id'] == $pid){
            $str .= '<div id="" class="com_con">';
           
            $str .= getstr($data, $v['pid'],$i);
            $i ++;
            $str .= '<span>'.$i.'</span><h5>'.$v['username'].'</h5>';
            $str .= '<p>'.$v['content'].'</p>';
            $url = U('Comment/commentBack',array('comment_id'=>$pid));
            $str .= '<div class="contact"><a onclick="good(this,'.$v['id'].')">顶[<span>'.$v['good_count'].'</span>]</a><a onclick="bad(this,'.$v['id'].')">踩[<span>'.$v['bad_count'].'</span>]</a><a onclick="contact_back(this)">回复</a></div>';
            $str .= '<div class="comment-box lou_comment" style="display: none;">
					<textarea placeholder="您的回复（必填）" name="content" id="comment-textarea" cols="100%" rows="3" tabindex="3"></textarea>
					<div class="comment-ctrl">
						<div class="comment-prompt" style="display: none;"> <i class="fa fa-spin fa-circle-o-notch"></i> <span class="comment-prompt-text">回复正在提交中...请稍后</span> </div>
						<div class="comment-success" style="display: none;"> <i class="fa fa-check"></i> <span class="comment-prompt-text">回复成功...</span> </div>';
            $str .= '<button type="button" onclick="comment(this,\''.$url.'\')" name="comment-submit" id="comment-submit" tabindex="4">回复</button>
					</div>
				</div>
						</div>';
        }
    }
    
    return $str;
}

?>